﻿using System;
using System.Collections.Generic;
using TaleWorlds.CampaignSystem.CharacterDevelopment;
using TaleWorlds.CampaignSystem.Extensions;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.Localization;
using TaleWorlds.SaveSystem;

namespace TaleWorlds.CampaignSystem.Party.PartyComponents
{
	// Token: 0x020002AF RID: 687
	public class VillagerPartyComponent : PartyComponent
	{
		// Token: 0x060027E1 RID: 10209 RVA: 0x000AAABC File Offset: 0x000A8CBC
		internal static void AutoGeneratedStaticCollectObjectsVillagerPartyComponent(object o, List<object> collectedObjects)
		{
			((VillagerPartyComponent)o).AutoGeneratedInstanceCollectObjects(collectedObjects);
		}

		// Token: 0x060027E2 RID: 10210 RVA: 0x000AAACA File Offset: 0x000A8CCA
		protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
		{
			base.AutoGeneratedInstanceCollectObjects(collectedObjects);
			collectedObjects.Add(this.Village);
		}

		// Token: 0x060027E3 RID: 10211 RVA: 0x000AAADF File Offset: 0x000A8CDF
		internal static object AutoGeneratedGetMemberValueVillage(object o)
		{
			return ((VillagerPartyComponent)o).Village;
		}

		// Token: 0x060027E4 RID: 10212 RVA: 0x000AAAEC File Offset: 0x000A8CEC
		public static MobileParty CreateVillagerParty(string stringId, Village village, int villagerPartySize)
		{
			return MobileParty.CreateParty(stringId, new VillagerPartyComponent(village), delegate(MobileParty mobileParty)
			{
				(mobileParty.PartyComponent as VillagerPartyComponent).InitializeVillagerPartyProperties(villagerPartySize);
			});
		}

		// Token: 0x17000A07 RID: 2567
		// (get) Token: 0x060027E5 RID: 10213 RVA: 0x000AAB1E File Offset: 0x000A8D1E
		// (set) Token: 0x060027E6 RID: 10214 RVA: 0x000AAB26 File Offset: 0x000A8D26
		[SaveableProperty(1)]
		public Village Village { get; private set; }

		// Token: 0x17000A08 RID: 2568
		// (get) Token: 0x060027E7 RID: 10215 RVA: 0x000AAB2F File Offset: 0x000A8D2F
		public override Hero PartyOwner
		{
			get
			{
				return this.Village.Settlement.OwnerClan.Leader;
			}
		}

		// Token: 0x17000A09 RID: 2569
		// (get) Token: 0x060027E8 RID: 10216 RVA: 0x000AAB46 File Offset: 0x000A8D46
		public override TextObject Name
		{
			get
			{
				if (this._cachedName == null)
				{
					this._cachedName = GameTexts.FindText("str_villagers_of_VILLAGE_NAME", null);
					this._cachedName.SetTextVariable("VILLAGE_NAME", this.Village.Name);
				}
				return this._cachedName;
			}
		}

		// Token: 0x17000A0A RID: 2570
		// (get) Token: 0x060027E9 RID: 10217 RVA: 0x000AAB83 File Offset: 0x000A8D83
		public override Settlement HomeSettlement
		{
			get
			{
				return this.Village.Settlement;
			}
		}

		// Token: 0x060027EA RID: 10218 RVA: 0x000AAB90 File Offset: 0x000A8D90
		protected internal VillagerPartyComponent(Village village)
		{
			this.Village = village;
		}

		// Token: 0x060027EB RID: 10219 RVA: 0x000AAB9F File Offset: 0x000A8D9F
		protected override void OnInitialize()
		{
			this.Village.VillagerPartyComponent = this;
		}

		// Token: 0x060027EC RID: 10220 RVA: 0x000AABAD File Offset: 0x000A8DAD
		protected override void OnFinalize()
		{
			this.Village.VillagerPartyComponent = null;
		}

		// Token: 0x060027ED RID: 10221 RVA: 0x000AABBB File Offset: 0x000A8DBB
		public override void ClearCachedName()
		{
			this._cachedName = null;
		}

		// Token: 0x060027EE RID: 10222 RVA: 0x000AABC4 File Offset: 0x000A8DC4
		private void InitializeVillagerPartyProperties(int villagerPartySize)
		{
			PartyTemplateObject villagerPartyTemplate = this.Village.Settlement.Culture.VillagerPartyTemplate;
			base.Party.MobileParty.Aggressiveness = 0f;
			Settlement bound = this.Village.Bound;
			bool flag;
			if (bound == null)
			{
				flag = (null != null);
			}
			else
			{
				Town town = bound.Town;
				flag = (((town != null) ? town.Governor : null) != null);
			}
			if (flag && this.Village.Bound.Town.Governor.GetPerkValue(DefaultPerks.Scouting.VillageNetwork))
			{
				villagerPartySize = MathF.Round((float)villagerPartySize * (1f + DefaultPerks.Scouting.VillageNetwork.SecondaryBonus));
			}
			if ((float)villagerPartySize > this.Village.Hearth)
			{
				villagerPartySize = (int)this.Village.Hearth;
			}
			this.Village.Hearth -= (float)((villagerPartySize + 1) / 2);
			base.Party.MobileParty.InitializeMobilePartyAroundPosition(villagerPartyTemplate, this.Village.Owner.Settlement.Position2D, 1f, 0f, villagerPartySize);
			base.Party.SetVisualAsDirty();
			base.Party.MobileParty.InitializePartyTrade(0);
			float num = 10000f;
			ItemObject itemObject = null;
			foreach (ItemObject itemObject2 in Items.All)
			{
				if (itemObject2.ItemCategory == DefaultItemCategories.PackAnimal && (float)itemObject2.Value < num && itemObject2.Value > 40)
				{
					itemObject = itemObject2;
					num = (float)itemObject2.Value;
				}
			}
			if (itemObject != null)
			{
				int amount = (int)(0.5f * (float)villagerPartySize);
				base.MobileParty.ItemRoster.Add(new ItemRosterElement(itemObject, amount, null));
			}
		}

		// Token: 0x04000C2B RID: 3115
		[CachedData]
		private TextObject _cachedName;
	}
}
